package com.knife.bbs.business;
/**
 * BBS权限管理信息
 * @author 蔡世友
 *
 */

import com.knife.web.tools.EasyDBODAO;
import com.knife.web.tools.IDAO;
import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.BBSDoc;
import com.knife.bbs.domain.Collections;
import com.knife.bbs.domain.FilterWords;
import com.knife.bbs.domain.Friend;
import com.knife.bbs.object.UserInfo;
import com.knife.bbs.domain.Vote;
import com.knife.bbs.domain.WebMessage;

public class BBSRights {
	private static BBSUserService userService=BBSUserService.getInstance();
	private static IDAO dao=EasyDBODAO.getInstance();
	public static boolean checkRights(Object obj,String Operater,ActiveUser user)
	{
		//System.out.println("checkRights");
		boolean ret=true;				
		if(obj instanceof BBSDoc)
		{
			ret=checkBBSDocRights((BBSDoc)obj,Operater,user);
		}
		else if(obj instanceof UserInfo)
		{
			ret=checkUserRights((UserInfo)obj,Operater,user);
		}
		else if(obj instanceof BBSDir)
		{
			ret=checkBBSDirRights((BBSDir)obj,Operater,user);
		}
		else if(obj instanceof FilterWords){
			ret=checkFilterWordsRights((FilterWords)obj,Operater,user);
		}
		else if(obj instanceof Vote){
			ret=checkAdministrator(user);
		}
		else if(obj instanceof Friend){
			ret=BBSUserRole.getUserRole((Friend)obj,user).isValidateUser();
		}
		else if(obj instanceof Collections){
			ret=BBSUserRole.getUserRole((Collections)obj,user).isValidateUser();
		}
		else if(obj instanceof WebMessage){
			ret=BBSUserRole.getUserRole((WebMessage)obj,user).isValidateUser();
		}
		return ret;
	}
	
	private static boolean checkFilterWordsRights(FilterWords fw,String operator,ActiveUser user){
		BBSUserRole role=BBSUserRole.getUserRole(fw,user);	
		if(operator.equals("add"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("del"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("edit"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("list"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		return true;
	}
	
	public static boolean checkAdministrator(ActiveUser user)
	{
		BBSUserRole role=BBSUserRole.getUserRole(null,user);
		return role.isAdministrator();
	}
	
	public static boolean checkActiveUser(ActiveUser user){
		BBSUserRole role=BBSUserRole.getUserRole(null,user);
		return role.isValidateUser();
	}
	
	private static boolean checkBBSDocRights(BBSDoc doc,String operator,ActiveUser user)
	{
		BBSUserRole role=BBSUserRole.getUserRole(doc,user);	
		if(operator.equals("add"))
		{
			if(doc.getParentid()!=null && (!doc.getParentid().equals(""))) return true;
			else
			return role.isValidateUser();
		}
		else if(operator.equals("del"))
		{
			return role.isAuthor()||role.isSubManager()||role.isManager()||role.isAdministrator();
		}
		else if(operator.equals("edit")||operator.equals("editMessage"))
		{
			//System.out.println("是否作者本人:"+role.isAuthor());
			return role.isAuthor()||role.isSubManager()||role.isManager()||role.isAdministrator();
		}
		else if(operator.equals("new"))
		{
			return role.isValidateUser();
		}
		else if(operator.equals("update"))
		{
			return role.isAuthor()||role.isSubManager()||role.isManager()||role.isAdministrator();
		}
		else if(operator.equals("reply"))
		{
			return role.isValidateUser();
		}
		else if(operator.equals("show"))
		{
			return true;
		}
		else if(operator.equals("read"))
		{
			if(doc.getVrtype()==null || doc.getVrtype().equals(""))return true;
			else if(user==null)
			{
				//doc.setContent("该帖只有论坛会员才能阅读!");
				return false;//未登录
			}
			UserInfo u=userService.getUserInfoByName(user.getUsername());
			return checkBBSReadRights(u,doc);
		}
		else if(operator.equals("setTopMessage"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("setEliteMessage"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("lockMessage"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("delMessage"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("moveMessage"))
		{
			return role.isAdministrator()||role.isManager()||role.isSubManager();
		}
		else if(operator.equals("lockUser"))
		{
			return role.isAdministrator();
		}
		return true;
	}
	
	private static boolean checkBBSDirRights(BBSDir dir,String operator,ActiveUser user)
	{
		BBSUserRole role=BBSUserRole.getUserRole(dir,user);
		if(operator.equals("add"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("del"))
		{
			return  role.isAdministrator();
		}
		else if(operator.equals("edit"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("new"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("update"))
		{
			return role.isAdministrator();
		}		
		else if(operator.equals("show"))
		{
			return true;
		}		
		return true;
	}
	private static boolean checkUserRights(UserInfo u,String operator,ActiveUser user)
	{
		BBSUserRole role=BBSUserRole.getUserRole(u,user);
		if(operator.equals("list"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("add"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("del"))
		{
			return role.isAuthor()||role.isAdministrator();
		}
		else if(operator.equals("edit"))
		{
			return role.isAuthor()||role.isAdministrator();
		}
		else if(operator.equals("new"))
		{
			return role.isAdministrator();
		}
		else if(operator.equals("update"))
		{
			return role.isAuthor()||role.isAdministrator();
		}		
		else if(operator.equals("show"))
		{
			return true;
		}	
		return true;
	}
	public static boolean checkBBSReadRights(UserInfo user,BBSDoc doc)
	{
		boolean ret=false;
		//会员
		if(doc.getVrtype().equals("user"))
		{
			ret=user!=null;
		}			
		else if(doc.getVrtype().equals("score"))//积分限制
		{
			ret= user.getScore().intValue()>new Integer(doc.getVrvalue()).intValue();

		}
		else if(doc.getVrtype().equals("reply"))
		{			
			String sql="select count(*) from BBSDoc where parentId=? and inputUser=?";
			java.util.Collection paras=new java.util.ArrayList();
			paras.add(doc.getCid());
			paras.add(user.getUsername());			
			Number num=(Number)dao.uniqueResult(sql,paras);			
			ret=num.intValue()>0;			
		}
		
		return ret;
	}
}
